package com.njupt.service.impl;

import com.njupt.mapper.ScoreMapper;
import com.njupt.po.Scoreinfo;
import com.njupt.service.ScoreService;
import com.njupt.util.Util;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ScoreServiceImpl implements ScoreService {

    @Resource
    private ScoreMapper scoreMapper;

    @Override
    public int insertScore(Scoreinfo scoreinfo) {
        Date now = new Date();
        SimpleDateFormat sdfEnrollTime = new SimpleDateFormat("yyyyMM");
        String formattedEnrollTime = sdfEnrollTime.format(now);
        String id = scoreMapper.getMaxID();
        if(id==null) {id = "0001";}
        else{ id= Util.getMaxScoreID(id);}
        id = "CJ"+formattedEnrollTime+id;
        scoreinfo.setId(id);
        SimpleDateFormat sdfOperationTime =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedOperationTime = sdfOperationTime.format(now);
        scoreinfo.setOperationTime(formattedOperationTime);
        return scoreMapper.insertScore(scoreinfo);
    }

    @Override
    public int deleteScore(String id) {
        return scoreMapper.deleteScore(id);
    }

    @Override
    public List<Scoreinfo> searchScore(String date, String name) {
        if(date==null && name==null ) {date="%%"; name="%%";
        System.out.println(date+","+name);}
        else if(date==null && name!=null) {date="%%"; name="%"+name+"%";
        System.out.println(date+","+name);}
        else if(date!=null && name==null) {name="%%"; date="%"+date+"%";
        System.out.println(date+","+name);}
        else{ date="%"+date+"%"; name="%"+name+"%";
        System.out.println(date+","+name);}
        return scoreMapper.searchScore(date,name);
    }
}
